package com.vil.controller;


import com.github.pagehelper.PageInfo;
import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.*;
import com.vil.service.CarsService;
import com.vil.service.PetService;
import com.vil.service.UserService;
import com.vil.service.VillageService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("ownerInformation")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CarsService cService;
    @Autowired
    private PetService petService;

    @Autowired
    private VillageService villageService;

    //去人员页面
    @RequestMapping("personnelManagement.html")
    public String managersList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("user", currentAdmin);
        ServerResponse serverResponse = userService.getListByManyConditions(1, 5, null);
        model.addAttribute("personnel", serverResponse.getData());
//        model.addAttribute("url","/administrator/adminlist.html");
        return "admin/pro_list";
    }

    @PostMapping("pro_List.html")
    public ServerResponse showUserList(Model model, @RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                           @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                           @RequestParam("condition") String  login_code,
                           @RequestParam("condition1") String  user_name){

             User user =new User();
             user.setLogin_code(login_code);
             user.setUser_name(user_name);
        return userService.getListByManyConditions(pageIndex, pageSize, user);
    }

    //去新增頁面
    @RequestMapping("personnel_add.html")
    public String personnel_add(Model model) {
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/personnel_add";
    }

    //增加
    @PostMapping("/addUser")
    @ResponseBody
    public ServerResponse addUserSelective(User user, HttpSession session) {

        return userService.insertSelective(user);
    }

    //去编辑页面
    @GetMapping("editUser.html/{id}")
    public String toEditUser(@PathVariable String id, Model model){
        ServerResponse byId =userService.getById(Long.valueOf(Integer.parseInt(id)));
        User data =(User) byId.getData();
        model.addAttribute("u_user",data);
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/useredit";
    }


    //修改
    @PostMapping("editUser")
    @ResponseBody
    public ServerResponse editUserSelective(User user){

        return userService.updateByPrimaryKeySelective(user);
    }

    @DeleteMapping("deleteUserById/{id}")
    @ResponseBody
    public ServerResponse deleteUserById(@PathVariable Long id){

        return  userService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteUserByIds")
    @ResponseBody
    public ServerResponse deleteUserByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return userService.deleteMany(array);
    }

    //去车辆页面vehicleManagement.html
    @RequestMapping("vehicleManagement.html")
    public String managersCarsList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("cars", currentAdmin);
        ServerResponse serverResponse = cService.getListByManyConditions(1, 5, null);
        model.addAttribute("cars", serverResponse.getData());
//        model.addAttribute("url","/administrator/adminlist.html");
        return "admin/vehicle_List";
    }

    @PostMapping("vehicle_List.html")
    public ServerResponse showCarsList(Model model, @RequestParam(value = "pageIndex",defaultValue = "1",required = false) Integer pageIndex,
                                       @RequestParam(value = "pageSize",defaultValue = "5",required = false) Integer pageSize,
                                       @RequestParam("condition") String  car_color,
                                       @RequestParam("condition1") String  owner){
                               Cars cars=new Cars();
                              cars.setCar_color(car_color);
                               cars.setCar_color(owner);
        return cService.getListByManyConditions(pageIndex, pageSize, cars);
    }

    //增加
    @PostMapping("/addCars")
    @ResponseBody
    public ServerResponse addCarsrSelective(Cars cars, HttpSession session) {

        return cService.insertSelective(cars);
    }
    //去新增頁面
    @RequestMapping("vehicle_add.html")
    public String vehicleadd(Model model) {
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());

        return "admin/vehicle_add";
    }
    //去编辑页面
    @GetMapping("editCars.html/{id}")
    public String toEditCars(@PathVariable String id, Model model){
        ServerResponse byId =cService.getById(Long.valueOf(Integer.parseInt(id)));
        Cars data =(Cars) byId.getData();
        model.addAttribute("u_cars",data);
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/carsedit";
    }


    //修改
    @PostMapping("editCars")
    @ResponseBody
    public ServerResponse editCarsSelective(Cars cars){

        return cService.updateByPrimaryKeySelective(cars);
    }

    @DeleteMapping("deleteCarsById/{id}")
    @ResponseBody
    public ServerResponse deleteCarsById(@PathVariable Long id){

        return  cService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteCarsByIds")
    @ResponseBody
        public ServerResponse deleteCarsByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return cService.deleteMany(array);
    }
    /**
     * 宠物管理
     */

    //去宠物页面
    @RequestMapping("petManagement.html")
    public String petManagement(Model model, HttpSession session){
        Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        ServerResponse serverResponse = petService.getListByManyConditions(1, 5, null);
        model.addAttribute("pet",serverResponse.getData());
        return "admin/pet_list";
    }
    //获取宠物列表
    @PostMapping("viewpetlist")
    @ResponseBody
    public ServerResponse viewdetaillist(@RequestParam(value = "condition",defaultValue = "",required = false) String owner,
                                         @RequestParam(value = "condition1",defaultValue = "",required = false) String pet_color,
                                         @RequestParam(value = "condition2",defaultValue = "",required = false) String pet_name,
                                         @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                         @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                         Model model){
        Pet pet = new Pet();
        pet.setOwner(owner);
        pet.setPet_color(pet_color);
        pet.setPet_name(pet_name);
        return petService.getListByManyConditions(pageIndex,pageSize,pet);

    }

    //增加
    @PostMapping("/addPet")
    @ResponseBody
    public ServerResponse addPetSelective(Pet pet, HttpSession session) {

        return petService.insertSelective(pet);
    }
    //去新增頁面
    @RequestMapping("pet_add.html")
    public String petadd(Model model) {
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/pet_add";
    }
    //去编辑页面
    @GetMapping("editpet.html/{id}")
    public String toEditPet(@PathVariable String id, Model model){
        ServerResponse byId =petService.getById(Long.valueOf(Integer.parseInt(id)));
        Pet data =(Pet) byId.getData();
        model.addAttribute("u_pet",data);
        ServerResponse serverResponse = villageService.getListVillage();
        model.addAttribute("vil",serverResponse.getData());
        return "admin/petedit";
    }


    //修改
    @PostMapping("editPet")
    @ResponseBody
    public ServerResponse editPetSelective(Pet pet){

        return petService.updateByPrimaryKeySelective(pet);
    }

    @DeleteMapping("deletepetById/{id}")
    @ResponseBody
    public ServerResponse deletePetById(@PathVariable Long id){

        return  petService.deleteByPrimaryKey(id);
    }

    @PostMapping("deletePetByIds")
    @ResponseBody
    public ServerResponse deletePetByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return petService.deleteMany(array);
    }

}
